package com.xyw.code.gateway.web.service.impl;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.xyw.code.authentication.client.service.IAuthService;
import com.xyw.code.gateway.web.service.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/4/22
 * Time: 下午2:17
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
@Service
@Slf4j
public class PermissionServiceImpl implements IPermissionService {

    /**
     * 由authentication-client模块提供签权的feign客户端
     */
    @Autowired
    private IAuthService authService;

    @Override
    @Cached(name = "gateway_auth::", key = "#authentication+#method+#url",
            cacheType = CacheType.LOCAL, expire = 10, timeUnit = TimeUnit.SECONDS, localLimit = 10000)
    public boolean auth(String authorization, String url, String method) {
        return authService.hasPermission(authorization, url, method);
    }
}
